/**
 * Created by guomin on 2017/5/8.
 */
var validator=require('validator');
var initPool=require('../common/initPool');
var checkParams=require('../common/checkParams');
var getData=require('../common/getData');
var jsonWrite=require('../conf/response');
var client=require('../conf/redis');

client.on("error",function(err){
    console.log("Error",err);
});

var resMsg=function(code,params){
    switch (code){
        case 0:return "SUCCESS";break;
        case 101:return "参数不全";break;
        case 102:return "参数格式错误";break;
        case 103:return "图片验证码错误";break;
        case 104:return "手机号格式错误";break;
        case 105:return "参数为空";break;
        case 107:return "手机验证码失效";break;
        case 108:return "手机验证码错误";break;
        case 201:return "服务端异常";break;
        case 204:return "不存在uid="+params.uid+"的用户";break;
        case 205:return "心愿不存在";break;
        case 206:return "心愿状态不是等待认领";break;
        case 207:return "同一用户认领超过2个心愿";break;
        default:return "其它错误";break;
    }
};

module.exports={
    getUserInfo:function(req,res){
        var date=new Date();
        var params=req.query;
        var uid=params.uid;
        checkParams.isDefined(params,["uid"]).then(function(){
            return checkParams.isParamsEmpty(params,["uid"]);
        }).then(function(){
            return checkParams.isRightFormat(params,["uid"]);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.getUserInfo(connection,uid);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":resMsg(0),"result":result,"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err,params),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //申请认领心愿
    claimWish:function(req,res){
        var date=new Date();
        var params=req.body;
        if(params.address==undefined){
            params.address=null;
        }
        params.wid=parseInt(params.wid);
        params.mobile=parseInt(params.mobile);
        console.log("claimwish:______",params);
            // jsonWrite(res,{"status":0,"msg":"完成申请提交","result":{"userid":5},"time":date.toLocaleString()});

        checkParams.isDefined(params,["wid","mobile","captcha","smsCode","name"]).then(function(){
            return checkParams.isParamsEmpty(params,["wid","mobile","captcha","smsCode","name"]);
        }).then(function(){
            return checkParams.isMobile(params.mobile);
        }).then(function() {
            return checkParams.isRightCaptcha(req,params.captcha);
        }).then(function(){
            return checkParams.smsIsValid(params,client);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.claimWish(connection,params);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":"完成申请提交","result":{"userid":result},"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err,params),"time":date.toLocaleString()});
                }else if(err.code==207){
                    jsonWrite(res,{"status":err.code,"msg":resMsg(err.code,params),"result":{"userid":err.uid},"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //假登录
    login:function(req,res){
        var date=new Date();
        var params=req.body;
        params.mobile=parseInt(params.mobile);
        console.log("login_______",params);
        checkParams.isDefined(params,["mobile","captcha","smsCode"]).then(function(){
            return checkParams.isParamsEmpty(params,["mobile","captcha","smsCode"]);
        }).then(function(){
            return checkParams.isMobile(params.mobile);
        }).then(function(){
            return checkParams.isRightCaptcha(req,params.captcha);
        }).then(function(){
            return checkParams.smsIsValid(params,client);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.login(connection,params);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":"SUCCESS","result":{"userid":result},"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err,params),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    },
    //获取认领人认领的心愿列表
    getClaimedWishList:function(req,res){
        var date=new Date();
        var params=req.query;
        checkParams.isDefined(params,["uid"]).then(function(){
            return checkParams.isParamsEmpty(params,["uid"]);
        }).then(function(){
            return checkParams.isRightFormat(params,["uid"]);
        }).then(function(){
            return initPool();
        }).then(function(connection){
            return getData.getClaimedWishList(connection,params);
        }).then(function(result){
            jsonWrite(res,{"status":0,"msg":resMsg(0),"result":result,"time":date.toLocaleString()});
        }).catch(function(err){
            if(err){
                var isInt=validator.isInt(err.toString());
                if(isInt){
                    jsonWrite(res,{"status":err,"msg":resMsg(err,params),"time":date.toLocaleString()});
                }else{
                    jsonWrite(res,{"status":201,"msg":resMsg(201),"time":date.toLocaleString()});
                    throw err;
                }
            }
        });
    }
};